// pages/offerDetails/offerDetails.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    like: true,
    noLike: "../../images/index/likebtnoff.png",
    isLike: "../../images/index/likebtnon.png",
    likePic: '', //点赞的图片
    collect: true,
    noCollect: "../../images/index/collectoff.png",
    isCollect: "../../images/index/collecton.png",
    collectPic: '', //收藏的图片
    isCollections: '', //是否收藏
    isThanks: '', //是否点赞
    tuser: '', //用户信息
    offer: [], //收到的offer
    standerSorce: [], //标化成绩
    comments: [], //评论
    currentOffer: {},
    showBottom:true,//显示底部
    showInput:false,//显示发布输入框
    comValue: '',
    getInput: '',
    scores:{}
  },

  // 我有话说
  writeComment:function(){
    let that = this;
    that.setData({
      showBottom: false,
      showInput: true
    })
  },
  // 关闭发布
  closeInput:function(){
    this.setData({
      showBottom: true,
      showInput: false
    })
  },
  // 发布输入框
  getInput: function (e) {
    this.setData({
      comValue: e.detail.value
    })
  },
  // 发布
  sendComment:function(){
    let getComment = this.data.comValue;
    // 发布成功
    this.setData({
      showBottom: true,
      showInput: false
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewComment = {}
    offerInterviewComment.commentatorId = userId
    offerInterviewComment.commentatorName = userName
    offerInterviewComment.beCommentatorId = this.data.tuser.id
    offerInterviewComment.beCommentatorName = this.data.tuser.nickName
    offerInterviewComment.offerInterviewId = this.data.currentOffer.id
    offerInterviewComment.comments = getComment
    offerInterviewComment.commentType = 1 //1offer2面经
    util.request(api.OfferIntevieCommentAdd, offerInterviewComment, 'POST', true).then(function (res) {
      console.log(res)
    })
  },
  cancelThaks: function(e) {
    let that = this;
    let isThanks = this.data.isThanks;
    if (isThanks) {
      that.setData({
        likePic: that.data.noLike
      })
    } else {
      that.setData({
        likePic: that.data.isLike
      })
    }
    this.setData({
      isThanks: !isThanks
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewThanksCollection = {}
    offerInterviewThanksCollection.collectionType = 1
    offerInterviewThanksCollection.offerInterviewId = this.data.currentOffer.id
    offerInterviewThanksCollection.thanksCollectionType = 1
    offerInterviewThanksCollection.userId = userId
    offerInterviewThanksCollection.userName = userName
    if (isThanks) {
      util.request(api.ThanksCollectionCancle, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    }else{
      util.request(api.ThanksCollectionAdd, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    }
  },
  // 收藏
  cancelCollect: function(e) {
    let that = this;
    let isCollections = this.data.isCollections;
    if (isCollections) {
      that.setData({
        collectPic: that.data.noCollect
      })
    } else {
      that.setData({
        collectPic: that.data.isCollect
      })
    }
    this.setData({
      isCollections: !isCollections
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewThanksCollection = {}
    offerInterviewThanksCollection.collectionType = 1
    offerInterviewThanksCollection.offerInterviewId = this.data.currentOffer.id
    offerInterviewThanksCollection.thanksCollectionType = 2
    offerInterviewThanksCollection.userId = userId
    offerInterviewThanksCollection.userName = userName
    if (isCollections) {
      util.request(api.ThanksCollectionCancle, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    } else {
      util.request(api.ThanksCollectionAdd, offerInterviewThanksCollection, 'POST', true).then(function (res) {
        console.log(res)
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    wx.request({
      url: api.OfferDetail + '?userId=' + userId + '&offerId=' + parseInt(options.offerId),
      method: 'GET',
      success: function(res) {
        let result = res.data.result;
        if (result.isThanks) {
          that.setData({
            likePic: that.data.isLike
          })
        } else {
          that.setData({
            likePic: that.data.noLike
          })
        }
        if (result.isCollections) {
          that.setData({
            collectPic: that.data.isCollect
          })
        } else {
          that.setData({
            collectPic: that.data.noCollect
          })
        }
        that.setData({
          isCollections: result.isCollections,
          isThanks: result.isThanks,
          tuser: result.offer.tuser,
          offer: result.offer.offers,
          standerSorce: result.offer.standerSorce,
          comments: result.comments,
          currentOffer: result.offer.offer,
          scores: result.offer.standerSorce == null ? {} : JSON.parse(result.offer.standerSorce.scores)
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(optons) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})